package io.renren.modules.jxgk.utils;

import io.renren.modules.jxgk.common.Constant;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.InputStream;
import java.util.UUID;

/**
 * Created by Administrator on 2017/11/20 0020.
 */
public class ImageFile {


    //图片上传
    public final static String saveImage(HttpServletRequest request, MultipartFile file)
    {
        String name= null;
        // 判断文件是否为空
        if (!file.isEmpty()) {
            try {
                String filename = file.getOriginalFilename();
                String prefix=filename.substring(filename.lastIndexOf("."));
                name=UUID.randomUUID().toString()+prefix;
                // 文件保存路径
                String Folder=request.getSession().getServletContext().getRealPath("/");
                String filePath=Folder+"upload\\";
              // String filePath="E:\\upload\\"+name;
                // 转存文件
                File filepath = new File(Folder, filename);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                file.transferTo(new File(filePath+name));

                return "upload/"+name;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return name;
    }
    public final static String saveFile(HttpServletRequest request, MultipartFile file) {

        String filename = file.getOriginalFilename();
        String prefix=filename.substring(filename.lastIndexOf("."));
        String name=UUID.randomUUID().toString()+prefix;
        String url=null;
        // 判断文件是否为空
        if (!file.isEmpty()) {
            try {
                // 文件保存路径
           //     String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + name;

                String filePath= Constant.IMAGE_PATH+name;
                 url=Constant.IMAGE_ADDRESS+name;
                // 转存文件
                file.transferTo(new File(filePath));
                return url;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return url;
    }




    //图片压缩并上传todo
    public final static String saveThumbnailsImage(HttpServletRequest request, MultipartFile file)
    {
        String urlofit="";
        String name= null;
        // 判断文件是否为空
        if (!file.isEmpty()) try {
            String filename = file.getOriginalFilename();
            String prefix = filename.substring(filename.lastIndexOf("."));
            name = UUID.randomUUID().toString() + prefix;
            // 文件保存路径
            String Folder = request.getSession().getServletContext().getRealPath("/");
            // String filePath=Folder+name;/
            String filePath = "E:\\upload\\" + name;


            // 转存文件
            File filepath = new File(Folder, filename);
            //判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }

            MultipartFile output = null;
            InputStream inputStream = file.getInputStream();
            BufferedImage src = ImageIO.read(inputStream);
            Image img20 = src.getScaledInstance(20, 20, Image.SCALE_DEFAULT);
            ImageIO.write((RenderedImage) img20, filePath, (File) output);
            output.transferTo(new File(filePath));
            urlofit = filePath;
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return name;
    }


}
